﻿using System;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace MouseAndCat.Model
{
    public class Slave
    {
        private static UdpClient _udp;
        //private bool _listen = true;
        private IPEndPoint _endPoint;

        public Slave(IPAddress ip, int port)
        {
            _endPoint = new IPEndPoint(ip, port);
            _udp = new UdpClient();
        }

        public void Start()
        {
            Debug.WriteLine(String.Format("Listening at {0}:{1}...", _endPoint.Address, _endPoint.Port));
            _udp.BeginReceive(ReceiveDataCallback, null);
        }

        private void ReceiveDataCallback(IAsyncResult result)
        {
            byte[] data = _udp.EndReceive(result, ref _endPoint);
            Debug.WriteLine("Data received: {0}", Encoding.ASCII.GetString(data));
        }

        public void Stop()
        {
            _udp.Close();
        }
    }
}
